package no.hig.imt4721.template;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import no.hig.imt4721.model.Repetition;
import no.hig.imt4721.model.Session;
import no.hig.imt4721.model.Subject;
import no.hig.imt4721.model.Template;
import no.hig.imt4721.util.Statistics;

public class TemplateCreatorMeanWithOutliers extends TemplateCreator
{	
	public TemplateCreatorMeanWithOutliers(int sessionsToDisregard, int sessionsForTemplate, int repetitionsForTemplate)
	{
		super(sessionsToDisregard, sessionsForTemplate, repetitionsForTemplate);
	}
	
	public void calculate(List<Subject> subjects)
	{
		splitData(subjects);
		
		// Create template
		for(Subject subject : getTemplateData())
		{
			Template template = new Template();
			
			for(int i = 0; i < subject.getSessions().get(0).getRepetitions().get(0).getLengthOfRow(); i++)
			{
				List<Double> values = new ArrayList<Double>();
				
				for(Session session : subject.getSessions())
				{
					for(Repetition repetition : session.getRepetitions())
					{
						values.add(repetition.getRowElement(i));
					}
				}
				
				double mean = Statistics.calculateMean(values);
				double standardDeviation = Statistics.calculateStandardDeviation(values);
				
				Iterator<Double> iter = values.iterator();
				
				while (iter.hasNext())
				{	
				    if ((Math.abs((iter.next().doubleValue() - mean)) / standardDeviation) > 3) 
				    	iter.remove();
				}
				
				mean = Statistics.calculateMean(values);
				template.addValue(mean);
			}
			
			templates.add(template);
		}
	}
}
